package org.sky.framework.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;

import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

public abstract class AbstractSwaggerConfig {

	@Bean
	public Docket customImplementation() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage(basePackage())).paths(PathSelectors.any()).build()
				.globalOperationParameters(showTokenParam());
	}

	protected String basePackage() {
		String className = AbstractSwaggerConfig.class.getName();
		String[] packages = className.split("\\.");
		return packages[0] + "." + packages[1];
	}

	protected abstract ApiInfo apiInfo();

	protected List<Parameter> showTokenParam() {
		List<Parameter> pars = new ArrayList<Parameter>();
		ParameterBuilder tokenPar = new ParameterBuilder();
		tokenPar.name("access_token").parameterType("query").modelRef(new ModelRef("string")).required(false).build();
		pars.add((Parameter) tokenPar.build());
		return pars;
	}

}
